package com.sjh.commons.listener;

import org.slf4j.MDC;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * 功能描述
 *
 * @author songjiahao
 * @date 2023/03/13  10:03
 */
public class LogMDCListener implements GenericApplicationListener {
    @Override
    public boolean supportsEventType(ResolvableType resolvableType) {
        return resolvableType.isAssignableFrom(ApplicationEnvironmentPreparedEvent.class);
    }

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        ApplicationEnvironmentPreparedEvent event = (ApplicationEnvironmentPreparedEvent) applicationEvent;
        ConfigurableEnvironment environment = event.getEnvironment();
        String property = environment.getProperty("spring.application.name");
        MDC.put("appName",property);
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE+19;
    }
}
